<template>
  <a-modal
    :title="title"
    :width="500"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

     <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="选择品类"
          hasFeedback >
                   <a-select
                  v-model="category"
                 style="width: 150px"
                @focus="focus"
                ref="select"
                @change="getallgongyi"
                placeholder="请选择品类"
              >
                 <a-select-option v-for="item in categoryList"  :key="item.id">
                      {{item.category_name}}
                      </a-select-option>
            </a-select>
        </a-form-item>

             <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="选择工艺"
          hasFeedback >
                   <a-select
                  v-model="gongyi"
                 style="width: 150px"
                @focus="focus"
                ref="select"
                @change="handleChange"
                placeholder="请选择工艺"
              >
                 <a-select-option v-for="item in gongyilis"  :key="item.id">
                      {{item.gongyiname}}
                      </a-select-option>
            </a-select>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
   import {getAction} from '@/api/manage';
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  export default {
    name: "addcost",
    data () {
      return {
        sysuserId:'',
        gongyi:'',
        category:'',
        gongyilis:{ //用来存放取到的工艺

        },
            categoryList:{ //用来存放取到的品类

        },
        discountId:'',
        id:'',
        discountRatio:'',
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: {
          add: "/TechnologyCostManage/addcostSystem",
          edit: "/TechnologyCostManage/addcostSystem",
          pingleilist:"/Discount/getallcategory",
          gongyilist:"/TechnologyCostManage/getallsongongyi"
        },
      }
    },
    created () {
      this.getallcategory();
      
    },
    methods: {
       //查询所有品类
          getallcategory(){
           getAction(this.url.pingleilist).then((res)=>{
              if(res.success){     
            this.categoryList=res.result;
              }else{
                that.$message.warning(res.message);
              } })
      },
         //查询该品类下的所有工艺
          getallgongyi(){
           getAction(this.url.gongyilist,{'parentId':this.category}).then((res)=>{
              if(res.success){
               this.gongyilis=res.result;
              }else{
                that.$message.warning(res.message);
              } })
      },
      add () {
        this.edit({});
      },
      edit (record,sysuserId) {
        this.sysuserId=sysuserId;
        
        this.discountRatio=record.discountRatio;
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','keyWord','sex','age','email','content'))
		      //时间格式化
          this.form.setFieldsValue({punchTime:this.model.punchTime?moment(this.model.punchTime,'YYYY-MM-DD HH:mm:ss'):null})
          this.form.setFieldsValue({birthday:this.model.birthday?moment(this.model.birthday):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'post';
            }
            let formData = Object.assign(this.model, values);

           formData['gongyi']=this.gongyi;
           formData['category']=this.category;
             formData['sysuserId']=this.sysuserId;
         
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                this.$emit('aaa');
              
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style scoped>

</style>